package com.cqeec.bootdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqeec.bootdemo.common.BaseContext;
import com.cqeec.bootdemo.common.R;
import com.cqeec.bootdemo.dto.OrderDto;
import com.cqeec.bootdemo.entity.OrderDetail;
import com.cqeec.bootdemo.entity.Orders;
import com.cqeec.bootdemo.entity.ShoppingCart;
import com.cqeec.bootdemo.service.OrderDetailService;
import com.cqeec.bootdemo.service.OrdersService;
import com.cqeec.bootdemo.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("下单成功");
    }
    @GetMapping("/userPage")
    public R<Page<OrderDto>> userPage(int page, int pageSize){
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        wrapper.orderByDesc(Orders::getCheckoutTime);
        Page<Orders> page1=ordersService.page(pageInfo,wrapper);
        //number 这个数据
        Page<OrderDto> dishDtoPage=new Page<>();
        BeanUtils.copyProperties(page1,dishDtoPage,"records");
        List<Orders> records=page1.getRecords();
        List<OrderDto> orderDtoList=new ArrayList<>();
        for (Orders order : records){
            OrderDto orderDto=new OrderDto();
            BeanUtils.copyProperties(order,orderDto);
            LambdaQueryWrapper<OrderDetail> wrapper1=new LambdaQueryWrapper<>();
            wrapper1.eq(OrderDetail::getOrderId,order.getId());
            List<OrderDetail> list=orderDetailService.list(wrapper1);
            orderDto.setOrderDetails(list);
            orderDtoList.add(orderDto);
        }
        dishDtoPage.setRecords(orderDtoList);
        return R.success(dishDtoPage);
    }
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders order) {
        // 1. 查询原订单的详情
        LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderDetail::getOrderId, order.getId());
        List<OrderDetail> orderDetails = orderDetailService.list(wrapper);

        // 2. 将商品重新加入购物车
        List<ShoppingCart> shoppingCartList = new ArrayList<>();
        for (OrderDetail detail : orderDetails) {
            ShoppingCart cart = new ShoppingCart();
            cart.setUserId(BaseContext.getCurrentId());
            cart.setName(detail.getName());
            cart.setImage(detail.getImage());
            cart.setAmount(detail.getAmount());
            cart.setNumber(detail.getNumber());
            if (detail.getDishId() != null) {
                cart.setDishId(detail.getDishId());
                cart.setDishFlavor(detail.getDishFlavor());
            } else {
                cart.setSetmealId(detail.getSetmealId());
            }
            shoppingCartList.add(cart);
        }
        // 3. 批量保存到购物车
        shoppingCartService.saveBatch(shoppingCartList);
        return R.success("已重新加入购物车");
    }
    @GetMapping("/page")
    public R<Page<Orders>> getPage(int page, int pageSize, String number,String beginTime,String endTime){
        log.info("number:{},beginTime:{},endTime:{}",number,beginTime,endTime);
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(number!=null,Orders::getNumber,number);
        wrapper.ge(beginTime!=null,Orders::getCheckoutTime,beginTime);
        wrapper.le(endTime!=null,Orders::getCheckoutTime,endTime);
        Page<Orders> page1=ordersService.page(pageInfo,wrapper);
        return R.success(page1);
    }
    @PutMapping
    public R<String> update(@RequestBody Orders order) {
        if (ordersService.updateById(order)) {
            return R.success("订单更新成功");
        }
        return R.error("订单更新失败");
    }


}
